package swing.chapter03;

import swing.util.SwingConsle;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *  弹出对话框
 */
public class Dialog extends JFrame {
    private JButton b1 = new JButton("Dialog Box");
    private MyDialog myDialog = new MyDialog(null);

    public Dialog(){
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 对话框必须调用 setVisible(true) 方法来显示和激活它
                myDialog.setVisible(true);
            }
        });

        add(b1);
    }

    public static void main(String[] args) {
        SwingConsle.run(new Dialog(), 200, 100);
    }

}

class MyDialog extends JDialog{

    public MyDialog(JFrame parent){
        super(parent, "My Dialog", true);
        setLayout(new FlowLayout());
        add(new JLabel("Here is my dialog"));

        JButton ok = new JButton("OK");
        ok.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 关闭对话框释放对话框使用的资源
                dispose();
            }
        });
        add(ok);

        setSize(150,125);
    }

}
